package hn.cch.controller;

import hn.cch.entity.Customer;
import hn.cch.entity.Industry;
import hn.cch.entity.Level;
import hn.cch.entity.Source;
import hn.cch.entity.User;
import hn.cch.mapper.CustomerMapper;
import hn.cch.mapper.IndustryMapper;
import hn.cch.mapper.LevelMapper;
import hn.cch.mapper.SourceMapper;
import hn.cch.mapper.UserMapper;
import hn.cch.page.PageData;
import hn.cch.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author CCH
 * @version 2018.09.27
 */
@Controller
@Transactional
@RequestMapping("/customer/")
public class CustomerController {

    /**
     * 这里省略了service层，controller层直接调用mapper层，特别配置看applicationContext.xml和dispatcher-config.xml
     *
     */

    @Autowired
    private CustomerService customerService;



    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LevelMapper levelMapper;

    @Autowired
    private SourceMapper sourceMapper;

    @Autowired
    private IndustryMapper industryMapper;


    @RequestMapping("customers")
    public String customers(Model model, Customer customer) {
        System.out.println("页面查询："+customer);

        //数据字典
        List<User> users = userMapper.selectAll();
        List<Level> levels = levelMapper.selectAll();
        List<Source> sources = sourceMapper.selectAll();
        List<Industry> industries = industryMapper.selectAll();
        model.addAttribute("users", users);
        model.addAttribute("levels", levels);
        model.addAttribute("sources", sources);
        model.addAttribute("industries", industries);

        //页面数据
        PageData<Customer> page = new PageData<>();
        //记录总数
        int count = customerMapper.selectCountByCustomer(customer);
        page.setCount(count);
        //默认设置:页面大小
        customer.setRows(PageData.SIZE);
        //页码赋值
        if (customer.getCode() == null) {
            //页码从1开始
            //记录从0开始
            customer.setCode(1);
            customer.setOffset(0);
        } else {
            customer.setOffset((customer.getCode() - 1) * PageData.SIZE);
        }
        page.setCode(customer.getCode());
        //记录数据
        System.out.println("查询条件："+customer);
        List<Customer> data = customerMapper.selectListByCustomer(customer);
        System.out.println("查询结果："+data);
        page.setData(data);
        //默认属性：page
        model.addAttribute("page", page);
        return "customers";
    }

    @RequestMapping("customer")
    @ResponseBody
    public Customer customer(Model model, int id) {
        Customer customer = customerMapper.selectCustomerById(id);
        System.out.println(customer);
        return customer;
    }
    @RequestMapping("insert")
    @ResponseBody
    public Customer insert(Customer customer) {
        //主键约束
        customer.setId(0);
        int ret = customerMapper.insertCustomer(customer);
        if (ret == 1){
            customer.setStatus(true);
        }else {
            customer.setStatus(false);
        }
        return customer;
    }

    @RequestMapping("update")
    @ResponseBody
    public Customer update(Customer customer) {
        int ret = customerMapper.updateCustomer(customer);
        if (ret == 1){
            customer.setStatus(true);
        }else {
            customer.setStatus(false);
        }
        return customer;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Customer delete(Customer customer) {
        int ret = customerMapper.deleteCustomer(customer);
        if (ret == 1){
            customer.setStatus(true);
        }else {
            customer.setStatus(false);
        }
        return customer;
    }


}
